﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;

namespace Honestysoft.Financing.WebUI.Module.Attach.Json
{
    /// <summary>
    /// FileCabinetTree 的摘要说明
    /// </summary>
    public class FileCabinetTree : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string parentId = context.Request.Params["id"];
            string functionId = context.Request.Params["FunctionID"];
            //string text = context.Request.Params["text"];
            DataTable dt = new Honestysoft.Financing.BusinessLogic.Attach("").GetFileCabinetTree(parentId);
            context.Response.Write(GetJson(dt, functionId));
        }

        private string GetJson(DataTable dt, string FunctionID)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            foreach (DataRow dr in dt.Rows)
            {
                sb.Append("{");
                sb.AppendFormat("\"id\":\"{0}\",", dr["ID"].ToString());
                sb.AppendFormat("\"name\":\"{0}\",",dr["Code"].ToString()+" "+ dr["Name"].ToString());
                sb.Append("\"target\":\"ifrmInfo\",");
                if (dr["NodeType"].ToString() == "0")
                {
                    sb.AppendFormat("\"icon\":\"{0}\",", "../../images/base.gif");
                    sb.AppendFormat("\"url\":\"{0}\",", "ProjectFileCabinet_List.aspx?FunctionID="+FunctionID);
                }
                else if (dr["NodeType"].ToString() == "1")
                {
                    sb.AppendFormat("\"icon\":\"{0}\",", "../../images/company.gif");
                    sb.AppendFormat("\"url\":\"{0}\",", "ProjectFileEdit2.aspx?ID="+dr["ID"].ToString()+"&FunctionID=" + FunctionID);
                }
                else if (dr["NodeType"].ToString() == "2")
                {
                    sb.AppendFormat("\"icon\":\"{0}\",", "../../images/book_red.png");
                    sb.AppendFormat("\"url\":\"{0}\",", "ContractFileEdit2.aspx?ID=" + dr["ID"].ToString() + "&FunctionID=" + FunctionID);
                }
                //sb.AppendFormat("\"icon\":\"{0}\",","");
                //sb.AppendFormat("\"url\":\"{0}\",", "");
                sb.AppendFormat("\"nodeType\":\"{0}\",", dr["NodeType"].ToString());
                sb.AppendFormat("\"isParent\":\"{0}\"", Convert.ToInt32(dr["ChildNodeCount"]) > 0 ? "true" : "false");
                sb.Append("},");
            }
            if (sb.ToString().EndsWith(","))
            {
                sb.Remove(sb.Length - 1, 1);
            }
            sb.Append("]");
            return sb.ToString();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}